﻿using System;
using System.Web.UI.WebControls;
using Service;

public partial class equipmentView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // txtHiddenEquipmentId.Text = Request.QueryString["equipmentId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;
        grdEquipment.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

        new Utility().SetRowToDisplay(grdEquipment, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {
            // to avoid loading for the first time
            if ((Request.QueryString["detail"] == null))
            {

                if ((Request.QueryString["delete"] == null))
                {
                    txtHiddenWhereClause.Text = "and Equipement.codeNumber='0'";
                    lblTotalEquipment.Text = "Total Equipments : 0";
                }
                else
                {

                    int equipmentId = Request.QueryString["equipmentId"] != null ? Int32.Parse(Request.QueryString["equipmentId"]) : 0;
                    new StockService().Delete(equipmentId);
                    txtHiddenWhereClause.Text = Session["whereClause"].ToString();
                    txthiddenOrderbyClause.Text = Session["orderByClause"].ToString();
                    lblTotalEquipment.Text = "Total Equipments : " + new DirectService().GetEquipementDisplayed(txtHiddenWhereClause.Text, txthiddenOrderbyClause.Text).Rows.Count;


                }
            }
            else
            {
                txtHiddenWhereClause.Text = Session["whereClause"].ToString();
                txthiddenOrderbyClause.Text = Session["orderByClause"].ToString();
                lblTotalEquipment.Text = "Total Equipments : " + new DirectService().GetEquipementDisplayed(txtHiddenWhereClause.Text, txthiddenOrderbyClause.Text).Rows.Count;
            }


            if (Session["pageIndex"] != null)
            {
                grdEquipment.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }


            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
            txtHiddenPageIndex.Text = "0";
        }
        else
        {
            lblTotalEquipment.Text = "Total Equipments : " + new DirectService().GetEquipementDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;

        }

    }

    protected void grdEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEquipment.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdEquipment.PageIndex.ToString();
        Session["pageIndex"] = grdEquipment.PageIndex;
    }
    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    protected void grdEquipment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdEquipment.EditIndex = -1;
    }
    protected void grdEquipment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdEquipment.EditIndex = e.NewEditIndex;
    }
    public string GetDisplayMode()
    {
        return Request.QueryString["detail"];
    }



    protected void grdEquipment_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void grdEquipment_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        }
    }
    protected void grdEquipment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        if (txtCodeNumber.Text != string.Empty)
        {
            and = " and ";
            str = "Equipement.codeNumber='" + txtCodeNumber.Text.Replace("'", "''").Trim() + "'";
        }

        // prepare by system
        if (drlSystem.SelectedIndex > 0)
        {
            str += and + "System.SystemID=" + drlSystem.SelectedValue;
            and = " and ";
        }

        // prepare by type equipement
        if (drlEquipmentType.SelectedIndex > 0)
        {
            str += and + "TypeEquipment.TypeEquipementID=" + drlEquipmentType.SelectedValue;
            and = " and ";
        }

        // prepare by model
        if (drlModel.SelectedIndex > 0)
        {
            str += and + "Model.ModelID=" + drlModel.SelectedValue;
            and = " and ";
        }

        // prepare by model
        if (drlCompany.SelectedIndex > 0)
        {
            str += and + "EquipmentPurchaseInformation.companyID=" + drlCompany.SelectedValue;
            and = " and ";
        }

        // prepare by user
        if (drlUser.SelectedIndex > 0)
        {
            str += and + "employee.employeeid=" + drlUser.SelectedValue;
            and = " and ";
        }
        if (!String.IsNullOrEmpty(str))
            whereClause = " and " + str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by Equipement.codeNumber";
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;

    }


    private void ResetSearchComponents()
    {
        drlCompany.SelectedIndex = 0;
        drlEquipmentType.SelectedIndex = 0;
        drlModel.SelectedIndex = 0;
        drlSystem.SelectedIndex = 0;
        drlUser.SelectedIndex = 0;
        txtCodeNumber.Text = string.Empty;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    protected void grdEquipment_Sorted(object sender, EventArgs e)
    {
        grdEquipment.DataBind();
    }
   
}
